package jicheng;

import java.io.IOException;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.xx.jicheng.entity.Category;
import com.xx.jicheng.entity.Product;
import com.xx.jicheng.mapper.CategoryMapper;
import com.xx.jicheng.mapper.ProductMapper;

public class SpiderProduct {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");
		CategoryMapper categoryMapper = context.getBean(CategoryMapper.class);
		ProductMapper productMapper = context.getBean(ProductMapper.class);
		List<Category> urls = categoryMapper.find1s();
		for (int k = 0; k < urls.size(); k++) {
			Category c = urls.get(k);
			String u = c.getUrl();
			String cid = c.getId();
			System.out.println("=========url is: ===========" + u);
			try {
				Document doc = Jsoup.connect(u).timeout(1000 * 120).get();
				int pages = SpiderProduct.getPages(doc);
				for (int i = 2; i <= pages; i++) {
					SpiderProduct.processOtherPages(u, i, productMapper, cid);
				}
				Elements elements = doc.select(".commodity_div_body_content");
				System.out.println("本页实际记录数:" + elements.size());
				for (Element e : elements) {
					SpiderProduct.processElement(e, productMapper, cid);
				}
			} catch (IOException e) {
				System.err.println("=========error url: ===========" + u);
			}
		}
		context.close();
	}

	static int getPages(Document doc) {
		Element script = doc.select("script").get(4);
		String[] lines = script.html().split("\\n");
		String pages = lines[0].replaceAll("^.*=(.*?);", "$1").replaceAll("\\s+", "");
		System.out.println("总页数:" + pages);
		System.out.println("总记录数:" + lines[1].replaceAll("^.*=(.*?);", "$1"));
		return Integer.parseInt(pages);
	}

	static void processOtherPages(final String originalurl, final int n, final ProductMapper productMapper,
			final String cid) {
		String newurl = originalurl + "&page=" + n;
		try {
			System.out.println("=========url is: ===========" + newurl);
			Document doc = Jsoup.connect(newurl).timeout(1000 * 120).get();
			Elements elements = doc.select(".commodity_div_body_content");
			System.out.println("本页实际记录数:" + elements.size());
			for (Element e : elements) {
				SpiderProduct.processElement(e, productMapper, cid);
			}
		} catch (IOException e) {
			System.err.println("=========error url: ===========" + newurl);
		}
	}

	static void processElement(Element e, ProductMapper productMapper, String cid) {
		try {
			String href = e.select(".commodity_img_div a").attr("href");
			String img = e.select(".commodity_img_div img").attr("src");
			String price = e.select(".commodity_img_div p").text().replace("￥", "");
			Elements ps = e.select(".commodity_info_div p");
			String brand = ps.get(0).text().replaceAll("品牌：", "");
			String category = ps.get(1).text().replaceAll("类别：", "");
			String type = ps.get(2).text().replaceAll("型号：", "");
			String id = e.select(".commodity_button_div p").attr("equi_id");
			// System.out.println("=========================================");
			// System.out.println(href);
			// System.out.println(img);
			// System.out.println(price);
			// System.out.println(brand);
			// System.out.println(category);
			// System.out.println(type);
			// System.out.println(id);
			Product p = new Product();
			p.setId(id);
			p.setType(type);
			p.setCategory(category);
			p.setBrand(brand);
			p.setPrice(price);
			p.setImg(img);
			p.setHref(href);
			p.setCid(cid);
			productMapper.save(p);
		} catch (Exception e1) {
			e1.printStackTrace();
			System.err.println("+++++++++++++++++");
		}
	}

}
